﻿#include "Test.h"
#include "XyNet/Crypto/Aes.h"
#include "XyNet/Crypto/Crc.h"
#include "XyNet/Crypto/Ecc.h"
#include "XyNet/Crypto/Pbkdf2.h"
#include "XyNet/Crypto/X917Random.h"

using namespace XyNet;

namespace XyNetTest
{
	void testAes(const string& originalText)
	{
		vector<shared_ptr<Aes>> aess = {
			make_shared<Aes>(Aes::Aes128), make_shared<Aes>(Aes::Aes192), make_shared<Aes>(Aes::Aes256) };

		vector<shared_ptr<thread>> threads;
		for (int i = 0; i < 20; ++i)
		{
			threads.emplace_back(make_shared<thread>([&originalText, &aess]()
			{
				try
				{
					for (int i = 0; i < 20; ++i)
					{
						for (auto& aes : aess)
						{
							XYNET_CHECK(aes->decrypt(aes->encrypt(originalText)) == originalText);
						}
					}
				}
				catch (const exception& e)
				{
					XYNET_HANDLE_EXCEPTION(e);
				}
			}));
		}
		for (auto t : threads)
		{
			t->join();
		}
	}

	void testEcc(const string& originalText)
	{
		Ecc::Decryptor eccDecryptor;
		Ecc::Encryptor eccEncryptor(eccDecryptor);
		Ecc::Encryptor eccEncryptor2(eccEncryptor.getPublicKey());

		vector<shared_ptr<thread>> threads;
		for (int i = 0; i < 100; ++i)
		{
			threads.emplace_back(make_shared<thread>(
				[&originalText, &eccDecryptor, &eccEncryptor, &eccEncryptor2]()
			{
				try
				{
					XYNET_CHECK(eccDecryptor.decrypt(eccEncryptor.encrypt(originalText)) == originalText);
					XYNET_CHECK(eccDecryptor.decrypt(eccEncryptor2.encrypt(originalText)) == originalText);
				}
				catch (const exception& e)
				{
					XYNET_HANDLE_EXCEPTION(e);
				}
			}));
		}
		for (auto t : threads)
		{
			t->join();
		}
	}

	void testCrypto()
	{
		string originalText = "传说中的中文，13579)*^$@，夹杂一些数字和符号。";
		System::consoleWriteLine(originalText);

		testAes(originalText);
		testEcc(originalText);
	}
}